package view.panel.emote
{
	import core.Control;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import global.Resource;
	
	import utils.ColorTool;
	import utils.DisplayTool;
	
	import view.components.TempBitmap;
	
	public class EmoteItem extends Sprite
	{
		private var img:DisplayObject;
		private var _key:String;
		private var _control:Control = Control.getInstance();
		private var _info:String;
		
		public function EmoteItem(imgKey:String)
		{
			initBoarder();
			
			if (imgKey)
			{
				setImg(imgKey);
			}
			else
			{
				this.mouseEnabled = false;
			}
			
			initListeners();
		}
		
		public function setImg(imgKey:String):void
		{
			_key = imgKey;
			img = Resource.getImg("emote_"+imgKey.toLocaleLowerCase());
			
			if (img is TempBitmap)
			{
				img.addEventListener(Event.COMPLETE,onImgComplete);
			}
			
			img.x = (this.width - img.width) * .5;
			img.y = (this.height - img.height) * .5;
			addChild(img);
			this.mouseEnabled = true;
			this.buttonMode = true;
		}
		
		private function onImgComplete(event:Event):void
		{
			DisplayTool.removeChild(this,img);
			event.target.removeEventListener(Event.COMPLETE,onImgComplete);
			img.x = (this.width - img.width) * .5;
			img.y = (this.height - img.height) * .5;
			addChild(img);
		}
		
		public function set enabled(v:Boolean):void
		{
			if (v)
			{
				ColorTool.saturation(this);
			}
			else
			{
				ColorTool.desaturation(this);	
			}
			
		}
		
		
		public function get key():String
		{
			return _key;
		}
		
		public function set info(v:String):void
		{
			_info = v;
		}
		
		private function initListeners():void
		{
			this.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			initBoarder();
			if (_info)
			{
				_control.hintControl.clear();
			}
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			init2Boarder();
			if (_info)
			{
				_control.hintControl.showHint(this,"<font color='#663300' size='12'>" + _info + "\n1$</font>");
			}
		}
		
		private function init2Boarder():void
		{
			with(this.graphics)
			{
				clear();
				lineStyle(2,0xFFFFFF);
				beginFill(0,.01);
				drawRoundRect(0,0,40,40,10,10);
				endFill();
			}
		}
		
		private function initBoarder():void
		{
			with(this.graphics)
			{
				clear();
				lineStyle(1,0xaaaaaa);
				beginFill(0,.01);
				drawRoundRect(0,0,40,40,6,6);
				endFill();
			}
		}
	}
}